<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib prefix="sys" tagdir="/WEB-INF/tags/sys"%>
<%@taglib uri="/WEB-INF/tlds/webui.tld" prefix="webui"%>

<webui:enum var="orgStatusEnum" code="orgStatus" />

<webui:panel>
		<div class="col-md-3">
			<div id="orgTree"></div>
		</div>
		<div class="col-md-9">
			<webui:table id="tb_orgs" title="组织列表"
				datas="_embedded.organizations" editable="true"	append="true"
				dataUrl="${contextPath}/rest/organizations/search/filter?page=0&size=10"
				postUrl="${contextPath}/sys/org/save.action">
				<webui:column editor="tableCheck" format="row.id"></webui:column>
				<webui:column field="orgId" editor="hidden" styleClass="display-none">{{row.orgId}}</webui:column>
				<webui:column field="name" editor="text" title="名称">{{row.name}}<webui:validator required="true"></webui:validator></webui:column>
				<webui:column field="code" editor="text" title="编码">{{row.code}}</webui:column>
				<webui:column field="parentId" editor="hidden" styleClass="display-none">{{row.parentId}}</webui:column>
				<webui:column field="status" lookup="${orgStatusEnum}" title="状态"><webui:validator selected="true"></webui:validator>{{row.statusDesc}}</webui:column>
				<webui:column title="操作" editor="opt">
					<button class="btn btn-xs btn-danger row-append-edit-btn-hide"  v-bind:onclick="'javascript:deleteOrg('+row.orgId+')'" type="button">删除</button>
				</webui:column>
			</webui:table>
		</div>
</webui:panel>

<script>
	var orgTree = null;

 	$(function () {
 		tb_orgs.completeEditRow = function(){
		    if(orgTree!=null){
			    orgTree.jstree(true).refresh();
			}
 		}
 		tb_orgs.initAppendRow = function(){
		    $("#code_new").val("org_" + mathRand(12));
		    $("#code_new").attr("disabled",true);
 		}
 		tb_orgs.completeAppendRow = function(){
		    $("#code_new").val("org_" + mathRand(12));
		    if(orgTree!=null){
			    orgTree.jstree(true).refresh();
			}
 		} 		
	    tb_orgs.initAppendRow();
	    
 	 	
 	 	orgTree = $('#orgTree').jstree({
        	'plugins' : [ 'types', 'dnd' ],
        	'core' : {
				'data' : {
						"url" : '${contextPath}/sys/org/tree.action',
						"data" : function (node) {
							return { "id" : node.id };
						}
					}
			},
			'types' : {
	            'default' : {
	                'icon' : 'fa fa-folder text-navy'
	            }
	        }
		});

		$('#orgTree').on("select_node.jstree", function(e,data) {
			$("#parentId_new").val(data.node.id);
			tb_orgs.url = "${contextPath}/rest/organizations/search/filter?parentId="+data.node.id;
			tb_orgs.reload();
	 	});
	});
	
	function deleteOrg(id){
 		confirm('确认删除组织?', function(result){
			$.ajax({
				method:"get",
				url:'${contextPath}/sys/org/delete/'+id+'.action',
				success:function(){
					tb_orgs.reload();
					orgTree.jstree(true).refresh();
				}
			});	
        });
	}
</script>